package com.wk.code.wkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wk.code.wkserver.entity.SysBanner;
import com.wk.code.wkserver.mapper.SysBannerMapper;
import com.wk.code.wkserver.request.admin.SysBannerRequest;
import com.wk.code.wkserver.response.base.PageInfo;
import com.wk.code.wkserver.service.ISysBannerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;


/**
 * SysBanner 实现层
 *
 * @Author wq
 * @Date 2021-06-12
 **/
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class SysBannerServiceImpl extends ServiceImpl<SysBannerMapper, SysBanner> implements ISysBannerService {


    @Override
    public PageInfo<SysBanner> page(SysBannerRequest request) {
        IPage<SysBanner> page = new Page<>(request.getPageNumber(), request.getPageSize());
        LambdaQueryWrapper<SysBanner> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(SysBanner::getSort);
        List<SysBanner> records = baseMapper.selectPage(page, wrapper).getRecords();
        if (!CollectionUtils.isEmpty(records)) {
            return new PageInfo<>(page.getTotal(), records);
        }
        return null;
    }

    @Override
    public void add(SysBannerRequest request) {
        SysBanner sysBanner = new SysBanner();
        BeanUtils.copyProperties(request, sysBanner);
        save(sysBanner);
    }

    @Override
    public void update(SysBannerRequest request) {
        SysBanner sysBanner = new SysBanner();
        BeanUtils.copyProperties(request, sysBanner);
        updateById(sysBanner);
    }

    @Override
    public void delete(Integer id) {
        baseMapper.deleteById(id);
    }

    @Override
    public SysBanner detail(Integer id) {
        return baseMapper.selectById(id);
    }
}